//
//  MsgViewController.m
//  MiAiApp
//
//  Created by voidcat on 2017/5/18.
//  Copyright © 2017年 voidcat. All rights reserved.
//

#import "MsgViewController.h"
#import "ContactUsViewController.h"
#import "RootWebViewController.h"
#import "EmitterViewController.h"
#import "ConversationListViewController.h"
#import "XLWebViewController.h"
#import "FansBaseViewController.h"
#import "ChatListViewTableViewCell.h"
@interface MsgViewController ()<QMUITableViewDelegate,QMUITableViewDataSource>
@property(nonatomic, strong) NSArray *dataSource;
@property(nonatomic, strong) NSArray *imageSourec;
@property(nonatomic, assign) BOOL isunread;
@end

@implementation MsgViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.isHidenNaviBar = NO;
    self.StatusBarStyle = UIStatusBarStyleDefault;
    self.title = ASLocalizedString(@"消息");
    self.isShowLiftBack = NO;
    self.tableView.frame = CGRectMake(0,0, SCREEN_WIDTH, SCREEN_HEIGHT - TabBarHeight);
    
    self.dataSource = @[ASLocalizedString(@"系统公告"),ASLocalizedString(@"系统消息"),ASLocalizedString(@"私信"),ASLocalizedString(@"关注"),ASLocalizedString(@"粉丝"),ASLocalizedString(@"联系我们")];
    self.imageSourec = @[@"systemNotice",@"message_home_system",@"message_home_private",@"message_home_follow",@"message_home_fans",@"icon_assistant"];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.mj_header.hidden = YES;
    self.tableView.mj_footer.hidden = YES;
    [self.view addSubview:self.tableView];
    [self.tableView reloadData];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataSource.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 65;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"cell";
    static NSString *systemID = @"cell";

    if([_dataSource[indexPath.row] isEqualToString:ASLocalizedString(@"私信")])
    {
        ChatListViewTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (!cell) {
            cell = [[ChatListViewTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        }
        cell.imageView.image = IMAGE_NAMED(_imageSourec[indexPath.row]);
        cell.textLabel.textColor = [UIColor blackColor];
        cell.textLabel.text = _dataSource[indexPath.row];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        if(_isunread)
        {
            cell.unread.hidden = NO;
        }
        else
        {
            cell.unread.hidden = YES;
        }
        return cell;
    }
    else
    {
        QMUITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:systemID];
        if (!cell) {
            cell = [[QMUITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:systemID];
        }
        cell.imageView.image = IMAGE_NAMED(_imageSourec[indexPath.row]);
        cell.textLabel.textColor = [UIColor blackColor];
        cell.textLabel.text = _dataSource[indexPath.row];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        return cell;
    }

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSString *cellName = _dataSource[indexPath.row];
    if([cellName isEqualToString:ASLocalizedString(@"私信")])
    {
        ConversationListViewController *vc = [ConversationListViewController new];
//        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:NO];
//        vc.hidesBottomBarWhenPushed = NO;
    }
    else if([cellName isEqualToString:ASLocalizedString(@"系统消息")])
    {
        XLWebViewController *systemMessage = [[XLWebViewController alloc] initWithUrl:KGlobalVariable.appmodel.app_h5.system_message];
        systemMessage.title = ASLocalizedString(@"系统消息");
        [self.navigationController pushViewController:systemMessage animated:YES];
    }
    else if([cellName isEqualToString:ASLocalizedString(@"关注")])
    {
        [self gotoAttrVC];
    }
    else if([cellName isEqualToString:ASLocalizedString(@"粉丝")])
    {
        [self gotoFansVC];
    }
    else if([cellName containsString:ASLocalizedString(@"联系我们")])
    {
        ContactUsViewController *vc = [[ContactUsViewController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    }
    else if([cellName containsString:ASLocalizedString(@"系统公告")])
    {
        NSString *url = [NSString stringWithFormat:@"http://www.yeying.mobi/mapi/public/index.php/api/notice_api/index?uid=%@", curUser.id];
        RootWebViewController *web = [[RootWebViewController alloc] initWithUrl:url];
        [self.navigationController pushViewController:web animated:YES];
    }
}

- (void)gotoQQ {
    NSString *QQ = KGlobalVariable.appmodel.custom_service_qq;
    //调用QQ客户端,发起QQ临时会话
    NSString *url = [NSString stringWithFormat:@"mqq://im/chat?chat_type=wpa&uin=%@&version=1&src_type=web",QQ];
    
    NSString *version= [UIDevice currentDevice].systemVersion;
    
    if(version.doubleValue >=9.0) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url] options:@{} completionHandler:^(BOOL success) {
            if(!success)
            {
                [HUDHelper alert:[NSString stringWithFormat:@"QQ:%@",KGlobalVariable.appmodel.custom_service_qq]];
            }
        }];
    }
    else
    {
        [HUDHelper alert:[NSString stringWithFormat:@"QQ:%@",KGlobalVariable.appmodel.custom_service_qq]];

    }
}

//跳往关注页面
-(void)gotoAttrVC
{
    FansBaseViewController *fans = [FansBaseViewController new];
    fans.type = CYFansTypeAttr;
    [self.navigationController pushViewController:fans animated:YES];
}
//跳往粉丝页面
-(void)gotoFansVC
{
    FansBaseViewController *fans = [FansBaseViewController new];
    fans.type = CYFansTypeFans;
    [self.navigationController pushViewController:fans animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    
}

-(void)viewWillAppear:(BOOL)animated
{
    
    NSUInteger unReadCount = [[IMAPlatform sharedInstance].conversationMgr unReadMessageCount];
    [UIApplication sharedApplication].applicationIconBadgeNumber = unReadCount;
    if(unReadCount > 0)
    {
        _isunread = YES;
    }
    else
    {
        _isunread = NO;
    }
    [super viewWillAppear:animated];
    [self.tableView reloadData];
    self.navigationController.navigationBar.hidden = NO;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
